package com.baseframe.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.log.exception.ServiceException;
import com.baseframe.score.entity.SnsScoreRules;
import com.baseframe.score.entity.SnsScoreUser;
import com.baseframe.score.entity.SnsSign;
import com.baseframe.score.mapper.SnsScoreRulesMapper;
import com.baseframe.score.mapper.SnsScoreUserMapper;
import com.baseframe.score.service.SnsScoreRulesService;
import com.baseframe.score.service.SnsScoreUserService;
import com.baseframe.score.service.SnsSignService;
import com.baseframe.score.mapper.SnsSignMapper;
import com.baseframe.score.sign.ScoreRule;
import com.baseframe.tool.result.R;
import com.baseframe.tool.utils.date.DateUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**

* @description 针对表【sns_sign(用户签到表)】的数据库操作Service实现
* @createDate 2023-06-26 00:45:34
*/
@Service
@AllArgsConstructor
public class SnsSignServiceImpl extends ServiceImpl<SnsSignMapper, SnsSign>
    implements SnsSignService{

    private SnsScoreRulesService snsScoreRulesService;
    private SnsScoreUserService snsScoreUserService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R<Object> signIn() {
        //判断是不是今天
        SnsSign snsSign =  this.baseMapper.getLastSign(AuthUtil.getUserId());

        if(snsSign != null){
            boolean today = DateUtil.isToday(snsSign.getSignTime());
            if (today){

                return R.fail("今天已经签到过了~");
            }
        }



        // 增加积分
        SnsScoreRules snsScoreRules = snsScoreRulesService.getOne(new LambdaQueryWrapper<SnsScoreRules>().eq(SnsScoreRules::getRewardItem, ScoreRule.SIGN_REWARD.getRewardItem()));

        SnsScoreUser snsScoreUser = snsScoreUserService.getOne(new LambdaQueryWrapper<SnsScoreUser>().eq(SnsScoreUser::getUserId, AuthUtil.getUserId()));

        boolean flag;
        if (snsScoreUser==null){
            snsScoreUser = new SnsScoreUser();
            snsScoreUser.setAccrueTotal(snsScoreRules.getRewardValue());
            snsScoreUser.setResidue(snsScoreRules.getRewardValue());
            flag = snsScoreUserService.save(snsScoreUser);

        }else {
            flag = snsScoreUserService.increaseScore(AuthUtil.getUserId(),snsScoreRules.getRewardValue());
        }

        if (!flag){
            throw new ServiceException(400,"用户积分更新失败~");
        }

        //新增签到记录
        SnsSign newSnsSign = new SnsSign();
        newSnsSign.setUserId(AuthUtil.getUserId());
        newSnsSign.setSignTime(new Date());

        return save(newSnsSign)?R.success(String.format("签到成功，积分+%d~",snsScoreRules.getRewardValue())):R.fail("签到失败~");
    }
}




